import Vue from 'vue'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import store from './vuex/store'
Vue.use(Vuex)

import VueCookies from 'vue-cookies'
Vue.use(VueCookies)

import Vant from 'vant';
import { Toast } from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

import AMap from 'vue-amap';  
Vue.use(AMap);

import $ from 'jquery';

// 全局注册组件
// import bindPhone from './components/bindPhone/bindPhone';
// Vue.use(bindPhone);

//全局注册过滤器
import filters from './common/vuefilter.js';
Object.keys(filters).forEach(key => {  
  Vue.filter(key, filters[key])  
})  




// 初始化vue-amap
AMap.initAMapApiLoader({
    // 高德key
    key: '50485ab84c936beb271937ac2e05b35a',
    // key: '657b30f6ee334d9dd973450ef17f8d70',
    // 插件集合 （插件按需引入）
    plugin: ['AMap.Geolocation','AMap.PlaceSearch']
});


import vuefilter from "./common/vuefilter"
import api from "./common/api"
import $axios from "./common/axios"
import echarts from "echarts";

Vue.prototype.$api = api;
Vue.prototype.$axios = $axios;
Vue.prototype.$vuefilter = vuefilter;
Vue.prototype.$echarts = echarts;
Vue.config.productionTip = false



//通过路由拦截，判断个别页面是否需要token
router.beforeEach((to, from, next) => {
  let initPath=to.fullPath;
  // return;
    // localStorage.setItem('curUrl',"http://"+window.location.host+to.fullPath);
    // localStorage.setItem('preUrl',"http://"+window.location.host+from.fullPath);
    var ua = window.navigator.userAgent.toLowerCase();
    // //判断是否是微信浏览器
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
      //判断是否已经授权登录
      if (!$cookies.get("token")&&to.name!="author") {
        // $cookies.set("shareUid",to.query.uid);
        $cookies.set("activity_id",to.query.activity_id);
        //已经配置的允许授权的域名
        let host = "m.yushin2018.com";
        //url为中间页地址，中间页面author必须要有
        let url = "http://" + host + "/author";
        let encodeurl = encodeURI(url);
        let redirectUrl =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfc31f3c5b0ea8f0b&redirect_uri=" +
          encodeurl +
          "&response_type=code&scope=snsapi_userinfo&state=" +
          to.fullPath +
          "#wechat_redirect";
        window.location.href = redirectUrl;
      } else {
        next(); 
      }
    } else {
      next();
    }

    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        var token =  $cookies.get('token');

        if (token) { // 通过vuex state获取当前的token是否存在
            next();
        } else {
            $cookies.set('beforeLoginUrl', to.fullPath);

            next({
                path: '/login',
                query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next();
    }
});


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})